<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li class="active"><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 初始化项目">1. 初始化项目</a><ul><li><a href="#t11.1 app/router.js">1.1 app/router.js</a></li><li><a href="#t21.2 app/controller/base.js">1.2 app/controller/base.js</a></li><li><a href="#t31.3 app/controller/resource.js">1.3 app/controller/resource.js</a></li><li><a href="#t41.4 app/controller/role.js">1.4 app/controller/role.js</a></li><li><a href="#t51.5 app/controller/roleResource.js">1.5 app/controller/roleResource.js</a></li><li><a href="#t61.6 app/controller/roleUser.js">1.6 app/controller/roleUser.js</a></li><li><a href="#t71.7 app/controller/user.js">1.7 app/controller/user.js</a></li><li><a href="#t81.8 app/service/base.js">1.8 app/service/base.js</a></li><li><a href="#t91.9 app/service/resource.js">1.9 app/service/resource.js</a></li><li><a href="#t101.10 app/service/role.js">1.10 app/service/role.js</a></li><li><a href="#t111.11 app/service/roleResource.js">1.11 app/service/roleResource.js</a></li><li><a href="#t121.12 app/service/roleUser.js">1.12 app/service/roleUser.js</a></li><li><a href="#t131.13 app/service/user.js">1.13 app/service/user.js</a></li><li><a href="#t141.14 config/config.default.js">1.14 config/config.default.js</a></li><li><a href="#t151.15 config/plugin.js">1.15 config/plugin.js</a></li></ul></li><li><a href="#t162. 设置权限">2. 设置权限</a><ul><li><a href="#t172.1 app/router.js">2.1 app/router.js</a></li><li><a href="#t182.2 controller/role.js">2.2 controller/role.js</a></li><li><a href="#t192.3 service/role.js">2.3 service/role.js</a></li></ul></li><li><a href="#t20参考">参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;">1. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE; <a href="#t01. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;"> # </a></h2>
<h3 id="t11.1 app/router.js">1.1 app/router.js <a href="#t11.1 app/router.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-params">app</span> =&gt;</span> {
  <span class="hljs-keyword">const</span> { router, controller } = app;
  router.resources(<span class="hljs-string">&apos;user&apos;</span>, <span class="hljs-string">&apos;/user&apos;</span>, controller.user);
  router.resources(<span class="hljs-string">&apos;role&apos;</span>, <span class="hljs-string">&apos;/role&apos;</span>, controller.role);
  router.resources(<span class="hljs-string">&apos;resource&apos;</span>, <span class="hljs-string">&apos;/resource&apos;</span>, controller.resource);
  router.resources(<span class="hljs-string">&apos;roleResource&apos;</span>, <span class="hljs-string">&apos;/roleResource&apos;</span>, controller.roleResource);
  router.resources(<span class="hljs-string">&apos;roleUser&apos;</span>, <span class="hljs-string">&apos;/roleUser&apos;</span>, controller.roleUser);
};
</code></pre>
<h3 id="t21.2 app/controller/base.js">1.2 app/controller/base.js <a href="#t21.2 app/controller/base.js"> # </a></h3>
<p>app/controller/base.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Controller = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>).Controller;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BaseController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span> </span>{
  <span class="hljs-keyword">async</span> success(data) {
    <span class="hljs-keyword">this</span>.ctx.body={
      <span class="hljs-attr">code</span>: <span class="hljs-number">0</span>,
      data
    }
  }
  <span class="hljs-keyword">async</span> error(error) {
    <span class="hljs-keyword">this</span>.ctx.body={
      <span class="hljs-attr">code</span>: <span class="hljs-number">1</span>,
      error
    }
  }
  <span class="hljs-comment">// &#x5206;&#x9875; &#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2; &#x6392;&#x5E8F;</span>
  <span class="hljs-keyword">async</span> index() {
    <span class="hljs-keyword">const</span> { app, ctx, service } = <span class="hljs-keyword">this</span>;
    <span class="hljs-keyword">const</span> {pageNum,pageSize,...where}=ctx.query;
    <span class="hljs-keyword">const</span> list=<span class="hljs-keyword">await</span> service[<span class="hljs-keyword">this</span>.model].list(pageNum,pageSize,where);
    <span class="hljs-keyword">const</span> total=<span class="hljs-keyword">await</span> service[<span class="hljs-keyword">this</span>.model].count(where);
    <span class="hljs-keyword">this</span>.success({
      list,
      total
    });
  }
  <span class="hljs-keyword">async</span> create() {
    <span class="hljs-keyword">const</span> { app, ctx, service } = <span class="hljs-keyword">this</span>;
    <span class="hljs-keyword">const</span> body = ctx.request.body;
    <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> service[<span class="hljs-keyword">this</span>.model].create(body);
    <span class="hljs-keyword">this</span>.success(result);
  }
  <span class="hljs-keyword">async</span> update() {
    <span class="hljs-keyword">const</span> { app, ctx, service } = <span class="hljs-keyword">this</span>;
    <span class="hljs-keyword">const</span> id = ctx.params.id;
    <span class="hljs-keyword">const</span> body = ctx.request.body;
    body.id = id;
    <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> service[<span class="hljs-keyword">this</span>.model].update(body);
    <span class="hljs-keyword">this</span>.success(result);
  }
  <span class="hljs-keyword">async</span> destroy() {
    <span class="hljs-keyword">const</span> { app, ctx, service } = <span class="hljs-keyword">this</span>;
    <span class="hljs-keyword">const</span> id=ctx.params.id;
    <span class="hljs-keyword">let</span> ids=ctx.request.body;
    <span class="hljs-keyword">if</span> (!ids) {
      ids=[id];
    }
    <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> service[<span class="hljs-keyword">this</span>.model].destroy(ids);
    <span class="hljs-keyword">this</span>.success(result);
  }
}

<span class="hljs-built_in">module</span>.exports = BaseController;
</code></pre>
<h3 id="t31.3 app/controller/resource.js">1.3 app/controller/resource.js <a href="#t31.3 app/controller/resource.js"> # </a></h3>
<p>app/controller/resource.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> BaseController = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseController</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;resource&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserController;
</code></pre>
<h3 id="t41.4 app/controller/role.js">1.4 app/controller/role.js <a href="#t41.4 app/controller/role.js"> # </a></h3>
<p>app/controller/role.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;

<span class="hljs-keyword">const</span> BaseController = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseController</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;role&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserController;
</code></pre>
<h3 id="t51.5 app/controller/roleResource.js">1.5 app/controller/roleResource.js <a href="#t51.5 app/controller/roleResource.js"> # </a></h3>
<p>app/controller/roleResource.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;

<span class="hljs-keyword">const</span> BaseController = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseController</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;roleResource&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserController;
</code></pre>
<h3 id="t61.6 app/controller/roleUser.js">1.6 app/controller/roleUser.js <a href="#t61.6 app/controller/roleUser.js"> # </a></h3>
<p>app/controller/roleUser.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;

<span class="hljs-keyword">const</span> BaseController = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseController</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;roleUser&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserController;
</code></pre>
<h3 id="t71.7 app/controller/user.js">1.7 app/controller/user.js <a href="#t71.7 app/controller/user.js"> # </a></h3>
<p>app/controller/user.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;

<span class="hljs-keyword">const</span> BaseController = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseController</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;user&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserController;
</code></pre>
<h3 id="t81.8 app/service/base.js">1.8 app/service/base.js <a href="#t81.8 app/service/base.js"> # </a></h3>
<p>app/service/base.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Service = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>).Service;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BaseService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Service</span> </span>{
    <span class="hljs-keyword">async</span> list(pageNum, pageSize, where) {
        <span class="hljs-keyword">const</span> {app}=<span class="hljs-keyword">this</span>;
        pageNum=<span class="hljs-built_in">isNaN</span>(pageNum)? <span class="hljs-number">1</span>:<span class="hljs-built_in">parseInt</span>(pageNum);
        pageSize=<span class="hljs-built_in">isNaN</span>(pageSize)? <span class="hljs-number">3</span>:<span class="hljs-built_in">parseInt</span>(pageSize);
        <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> app.mysql.select(<span class="hljs-keyword">this</span>.model, {
            where,
            <span class="hljs-attr">orders</span>: [[<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;desc&apos;</span>]],
            <span class="hljs-attr">offset</span>: (pageNum - <span class="hljs-number">1</span>) * pageSize,
            <span class="hljs-attr">limit</span>: pageSize,
        });
        <span class="hljs-keyword">return</span> result;
    }
    <span class="hljs-keyword">async</span> count(where) {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.app.mysql.count(<span class="hljs-keyword">this</span>.model,where);
    }
    <span class="hljs-keyword">async</span> create(user) {
        <span class="hljs-keyword">const</span> {app}=<span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> app.mysql.insert(<span class="hljs-keyword">this</span>.model, user);
        <span class="hljs-keyword">const</span> affectedRows = result.affectedRows;
        <span class="hljs-keyword">return</span> affectedRows &gt; <span class="hljs-number">0</span> ? result.insertId : <span class="hljs-string">&apos;error&apos;</span>;
    }
    <span class="hljs-keyword">async</span> update(updateUser) {
        <span class="hljs-keyword">const</span> { app } = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> app.mysql.update(<span class="hljs-keyword">this</span>.model, updateUser);
        <span class="hljs-keyword">const</span> affectedRows = result.affectedRows;
        <span class="hljs-keyword">return</span> affectedRows &gt; <span class="hljs-number">0</span> ? <span class="hljs-string">&apos;success&apos;</span> : <span class="hljs-string">&apos;error&apos;</span>;
    }
    <span class="hljs-keyword">async</span> destroy(ids) {
        <span class="hljs-keyword">const</span> { app } = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> app.mysql.delete(<span class="hljs-keyword">this</span>.model, { <span class="hljs-attr">id</span> :ids});
        <span class="hljs-keyword">const</span> affectedRows = result.affectedRows;
        <span class="hljs-keyword">return</span> affectedRows &gt; <span class="hljs-number">0</span> ? <span class="hljs-string">&apos;success&apos;</span> : <span class="hljs-string">&apos;error&apos;</span>;
    }
}

<span class="hljs-built_in">module</span>.exports = BaseService;
</code></pre>
<h3 id="t91.9 app/service/resource.js">1.9 app/service/resource.js <a href="#t91.9 app/service/resource.js"> # </a></h3>
<p>app/service/resource.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;
<span class="hljs-keyword">const</span> BaseService = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseService</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;resource&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserService;
</code></pre>
<h3 id="t101.10 app/service/role.js">1.10 app/service/role.js <a href="#t101.10 app/service/role.js"> # </a></h3>
<p>app/service/role.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> BaseService = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RoleService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseService</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;role&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = RoleService;
</code></pre>
<h3 id="t111.11 app/service/roleResource.js">1.11 app/service/roleResource.js <a href="#t111.11 app/service/roleResource.js"> # </a></h3>
<p>app/service/roleResource.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> BaseService = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RoleResourceService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseService</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;role_resource&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = RoleResourceService;
</code></pre>
<h3 id="t121.12 app/service/roleUser.js">1.12 app/service/roleUser.js <a href="#t121.12 app/service/roleUser.js"> # </a></h3>
<p>app/service/roleUser.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;
<span class="hljs-keyword">const</span> BaseService = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseService</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;role_user&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserService;
</code></pre>
<h3 id="t131.13 app/service/user.js">1.13 app/service/user.js <a href="#t131.13 app/service/user.js"> # </a></h3>
<p>app/service/user.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> BaseService = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseService</span> </span>{
  <span class="hljs-keyword">constructor</span>(...args) {
    <span class="hljs-keyword">super</span>(...args);
    <span class="hljs-keyword">this</span>.model = <span class="hljs-string">&apos;user&apos;</span>;
  }
}

<span class="hljs-built_in">module</span>.exports = UserService;
</code></pre>
<h3 id="t141.14 config/config.default.js">1.14 config/config.default.js <a href="#t141.14 config/config.default.js"> # </a></h3>
<p>config/config.default.js</p>
<pre><code class="lang-js"><span class="hljs-meta">&apos;use strict&apos;</span>;

<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-params">appInfo</span> =&gt;</span> {
  <span class="hljs-keyword">const</span> config = exports = {};

  <span class="hljs-comment">// use for cookie sign key, should change to your own and keep security</span>
  config.keys = appInfo.name + <span class="hljs-string">&apos;_1536483095408_1038&apos;</span>;

  <span class="hljs-comment">// add your config here</span>
  config.middleware = [];

  config.mysql = {
    <span class="hljs-attr">client</span>: {
      <span class="hljs-attr">host</span>: <span class="hljs-string">&apos;localhost&apos;</span>,
      <span class="hljs-attr">port</span>: <span class="hljs-number">3306</span>,
      <span class="hljs-attr">user</span>: <span class="hljs-string">&apos;root&apos;</span>,
      <span class="hljs-attr">password</span>: <span class="hljs-string">&apos;&apos;</span>,
      <span class="hljs-attr">database</span>: <span class="hljs-string">&apos;zhufeng-cms&apos;</span>,
    },
    <span class="hljs-attr">app</span>: <span class="hljs-literal">true</span>, <span class="hljs-comment">// app.mysql</span>
    agent: <span class="hljs-literal">false</span>,
  };
  config.security = {
    <span class="hljs-attr">csrf</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">whiteList</span>: [ <span class="hljs-string">&apos;http://localhost:8000&apos;</span> ],
  };
  <span class="hljs-keyword">return</span> config;
};
</code></pre>
<h3 id="t151.15 config/plugin.js">1.15 config/plugin.js <a href="#t151.15 config/plugin.js"> # </a></h3>
<p>config/plugin.js</p>
<pre><code class="lang-js">exports.mysql = {
  <span class="hljs-attr">enable</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">package</span>: <span class="hljs-string">&apos;egg-mysql&apos;</span>,
};

exports.cors = {
  <span class="hljs-attr">enable</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">package</span>: <span class="hljs-string">&apos;egg-cors&apos;</span>,
};
</code></pre>
<h2 id="t162. &#x8BBE;&#x7F6E;&#x6743;&#x9650;">2. &#x8BBE;&#x7F6E;&#x6743;&#x9650; <a href="#t162. &#x8BBE;&#x7F6E;&#x6743;&#x9650;"> # </a></h2>
<h3 id="t172.1 app/router.js">2.1 app/router.js <a href="#t172.1 app/router.js"> # </a></h3>
<p>app/router.js</p>
<pre><code class="lang-js">router.get(<span class="hljs-string">&apos;/role/getResources&apos;</span>,controller.role.getResources);
router.post(<span class="hljs-string">&apos;/role/setRolePermission&apos;</span>,controller.role.setRolePermission);
</code></pre>
<h3 id="t182.2 controller/role.js">2.2 controller/role.js <a href="#t182.2 controller/role.js"> # </a></h3>
<p>app/controller/role.js</p>
<pre><code class="lang-js">  <span class="hljs-keyword">async</span> getResources() {
    <span class="hljs-keyword">let</span> resources=<span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.service[<span class="hljs-keyword">this</span>.model].getResources();
    <span class="hljs-keyword">this</span>.success(resources);
  }
  <span class="hljs-keyword">async</span> setRolePermission() {
    <span class="hljs-keyword">let</span> {
        ctx,
        service
    } = <span class="hljs-keyword">this</span>;
      <span class="hljs-keyword">let</span> body=ctx.request.body;
      <span class="hljs-keyword">await</span> service[<span class="hljs-keyword">this</span>.model].setRolePermission(body);
      <span class="hljs-keyword">this</span>.success(<span class="hljs-string">&apos;&#x8BBE;&#x7F6E;&#x6743;&#x9650;&#x6210;&#x529F;&apos;</span>);
  }
  <span class="hljs-keyword">async</span> getUsers() {
    <span class="hljs-keyword">let</span> users=<span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.service[<span class="hljs-keyword">this</span>.model].getUsers();
    <span class="hljs-keyword">this</span>.success(users);
  }
</code></pre>
<h3 id="t192.3 service/role.js">2.3 service/role.js <a href="#t192.3 service/role.js"> # </a></h3>
<p>app/service/role.js</p>
<pre><code class="lang-js">  <span class="hljs-keyword">async</span> list(pageNum, pageSize, where) {
    <span class="hljs-keyword">const</span> {app}=<span class="hljs-keyword">this</span>;
    pageNum=<span class="hljs-built_in">isNaN</span>(pageNum)? <span class="hljs-number">1</span>:<span class="hljs-built_in">parseInt</span>(pageNum);
    pageSize=<span class="hljs-built_in">isNaN</span>(pageSize)? <span class="hljs-number">3</span>:<span class="hljs-built_in">parseInt</span>(pageSize);
    <span class="hljs-keyword">const</span> roles = <span class="hljs-keyword">await</span> app.mysql.select(<span class="hljs-keyword">this</span>.model, {
        where,
        <span class="hljs-attr">orders</span>: [[<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;desc&apos;</span>]],
        <span class="hljs-attr">offset</span>: (pageNum - <span class="hljs-number">1</span>) * pageSize,
        <span class="hljs-attr">limit</span>: pageSize,
    });
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;roles.length;i++){
      <span class="hljs-keyword">let</span> rows=<span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.app.mysql.query(<span class="hljs-string">`select resource_id from role_resource where role_id=?`</span>,[roles[i].id]);
      roles[i].resourceIds=rows.map(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item.resource_id);
      <span class="hljs-comment">//rows=await this.app.mysql.query(`select user_id from role_user where role_id=?`,[roles[i].id]);</span>
      <span class="hljs-comment">//roles[i].users=rows.map(item=&gt;item.user_id);</span>
    }
    <span class="hljs-keyword">return</span> roles;
}
  <span class="hljs-keyword">async</span> getUsers() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.app.mysql.select(<span class="hljs-string">&apos;user&apos;</span>);
  }
  <span class="hljs-keyword">async</span> getResources() {
    <span class="hljs-keyword">let</span> resources = <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.app.mysql.select(<span class="hljs-string">&apos;resource&apos;</span>);
    <span class="hljs-keyword">let</span> map={};
    <span class="hljs-keyword">let</span> top=[];
    resources=resources.filter(<span class="hljs-function"><span class="hljs-params">resource</span> =&gt;</span> {
      resource.children=[];
      map[resource.id]=resource;
      <span class="hljs-keyword">if</span> (resource.parent_id == <span class="hljs-number">0</span>) {
        top.push(resource);
      } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
      }
    });
    resources.forEach(<span class="hljs-function"><span class="hljs-params">resource</span> =&gt;</span> {
      <span class="hljs-keyword">let</span> parentId=resource.parent_id;
      map[parentId].children.push(resource);
    });
    <span class="hljs-keyword">return</span> top;
  }
  <span class="hljs-keyword">async</span> setRolePermission({roleId,resourceIds}) {
    <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.app.mysql.query(<span class="hljs-string">`delete from role_resource where role_id =?`</span>,[roleId]);
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;resourceIds.length;i++){
      <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.app.mysql.query(<span class="hljs-string">`insert into role_resource(role_id,resource_id) values(?,?)`</span>,[roleId,<span class="hljs-built_in">parseInt</span>(resourceIds[i])]);
    }
  }
</code></pre>
<h2 id="t20&#x53C2;&#x8003;">&#x53C2;&#x8003; <a href="#t20&#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://github.com/ali-sdk/ali-rds">ali-rds</a></li>
<li><a href="https://gitee.com/zhufengpeixun/zhufeng-cms2">zhufeng-cms2</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
